<?
class ModPbnySummaryHelper
{
    function getVenues(){
		$result = array();
		
		$venue  = JModel::getInstance( 'venue', 'venuemodel' );
		$venues = $venue->get();
		if($venues)
			foreach($venues as $venue)
				$result[$venue->id] = $venue;
			
		return $result;
    }
	
	function getTeams(){
		$result = array();
		
		$team   = JModel::getInstance( 'team', 'teammodel' );
		$teams  = $team->get();
		if($teams)
			foreach($teams as $team)
				$result[$team->id] = $team;
		
		return $result;
    }
	
	function getGamesSummaries(){
		$mseason = JModel::getInstance( 'season', 'seasonmodel' );
		$season  = $mseason->get_latest();
		
		$mschedule = JModel::getInstance( 'schedule', 'schedulemodel' );
		
		$weeks   = ModPbnySummaryHelper::get_weeks($season);
		for($i=0; $i<count($weeks); $i++){
			$week      = $weeks[$i];
			$schedules = $mschedule->get_by_schedule($week->start, $week->end);
			if($schedules)
				$week->game_schedules = ModPbnySummaryHelper::prepare_schedules($schedules);
		}
		
		return $weeks;
	}
	
	private static function get_weeks($season){
		$result = array();
		if(!$season)
			return $result;
			
		$start  = strtotime($season->start_date);
		$end    = strtotime($season->end_date);
		$sec_wk = 60*60*24*7;
		$weeks  = floor(($end - $start)/$sec_wk);
		
		for($i = 0; $i < $weeks; $i++){
			$result[$i] = new stdClass();
			$result[$i]->start = strftime('%Y-%m-%d', $start + ($i * $sec_wk));
			$result[$i]->end   = strftime('%Y-%m-%d', $start + (($i + 1) * $sec_wk));
		}
		
		return $result;
	}
	
	private static function prepare_schedules($schedules){
		
		$result = array();
		$gs    = JModel::getInstance('gamestat','gamestatmodel');
		$ges   = JModel::getInstance('gameendstatus','gameendstatusmodel');

		foreach($schedules as $schedule){
			$stats = $gs->get_by($schedule->id);
			if($stats){
				$score = array();
				foreach($stats as $s){
					$score[$schedule->first_team_id]  = $s->first_team_final_score;
					$schedule->game_end_status        = $ges->get($s->game_end_status_id);
					$score[$schedule->second_team_id] = $s->second_team_final_score;
				}
				$schedule->scores = $score;
			}
			
			$date = strftime('%Y-%m-%d', strtotime($schedule->schedule));
			if(!isset($result[$date]))
				$result[$date] = array();
			
			$schedule->division = ModPbnySummaryHelper::get_division($schedule);
			$result[$date][$schedule->id] = $schedule;
		}
		
		foreach($result as $key => $a){
			$result[$key] = ModPbnySummaryHelper::subval_sort($a, 'schedule');
		}
		
		return $result;
	}
	
	## PRIVATE METHODS:
	private function subval_sort($a,$subkey) {
	
		if(empty($a))
			return array();
			
		foreach($a as $k=>$v) {
			$b[$k] = strtolower($v->$subkey);
		}
		asort($b);
	
		foreach($b as $key=>$val) {
			$c[$key] = $a[$key];
		}
	
		return $c;
	}
	
	private function get_division($schedule){
		$division  = JModel::getInstance('division', 'divisionmodel');
		
		# get division for the schedule
		$division_result = array();
		foreach(array($schedule->first_team_id, $schedule->second_team_id) as $tid){
			
			$divisions = $division->get_by_team_id($tid);
			$division_result[$tid] = array();
			
			foreach($divisions as $d)
				array_push($division_result[$tid], $d->id);
		}
		$divisions = array_intersect($division_result[$schedule->first_team_id],
									 $division_result[$schedule->second_team_id]);

		if(!empty($divisions))
			return $division->get($divisions[0]);
	}
}
?>